import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class GradeInfArray {
    ArrayList<Grade> gradesArrayList = new ArrayList<>();
    public void gradesInfArrayInit()
    {
        String gradesInfArray = "src/GradeInfArray";
        try {
            // 读取文件内容到字符串列表
            List<String> lines = Files.readAllLines(Paths.get(gradesInfArray), StandardCharsets.UTF_8);

            // 遍历字符串列表并输出每一行
            for (String line : lines) {
                // 使用空格分隔每行数据
                String[] split = line.split("\\s+");
                if (split.length >= 7) {
                    Grade newgrade = new Grade();
                    newgrade.setName(split[0]);
                    newgrade.setgID(split[1]);
                    newgrade.setTeacherName(split[2]);
                    newgrade.setTeacherID(split[3]);
                    newgrade.setStudentName(split[4]);
                    newgrade.setStudentID(split[5]);
                    newgrade.setScore((split[6]));
                    gradesArrayList.add(newgrade);
                    // 其他操作
                } else {
                    // 处理不符合格式的行
                    continue;
                }
            }
        } catch (IOException e) {
            // 处理异常

        }
    }

    public void CourseInfArrayUpdate() {
        String gradesInfArray = "src/GradeInfArray";
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(gradesInfArray))) {
            // 遍历HashMap并将键值对写入文件
            for (Grade grade : gradesArrayList) {
                writer.write(grade.getName() + " ");
                writer.write(grade.getgID() + " ");
                writer.write(grade.getTeacherName() + " ");
                writer.write(grade.getTeacherID() + " ");
                writer.write(grade.getStudentName() + " ");
                writer.write(grade.getStudentID()+ " ");
                writer.write(grade.getScore() );
            }
//            System.out.println("ArrayList contents have been written to the file.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public ArrayList<Grade> gradeDataSheetGet(boolean a)
    {
        return gradesArrayList;
    }


}
